Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get character from script? (Tool)

Asked by
BiIinear 104
5 years ago

I've been struggling on this for half an hour, I'm not sure how to get the character from a normal script. The script is located inside a tool.

script.Parent.Equip.Player.Value = game.Players:FindFirstChild(script.Parent.Parent.Parent.Name)

if not game.ReplicatedStorage:FindFirstChild("spellcast_event") then
    script.Spellcast_event.Parent = game.ReplicatedStorage
    script.Parent.Output.Disabled = false
else
    script.Spellcast_event:Destroy()
    script.Parent.Output.Disabled = false
end
wait(1)
script:Destroy()

And this is the script where I'm getting the error from.

When I equip: 23:08:54.482 - Players.BiIinear.Backpack.Spellcast.Equip:7: attempt to index global 'player' (a nil value) 23:08:54.483 - Stack Begin 23:08:54.483 - Script 'Players.BiIinear.Backpack.Spellcast.Equip', Line 7 23:08:54.483 - Stack End

When I unequip: 23:08:55.581 - Players.BiIinear.Backpack.Spellcast.Equip:13: attempt to index global 'player' (a nil value) 23:08:55.581 - Stack Begin 23:08:55.582 - Script 'Players.BiIinear.Backpack.Spellcast.Equip', Line 13 23:08:55.582 - Stack End

player = game.Players:FindFirstChild(script.Player.Value)
handle = script.Parent.Handle

script.Parent.Equipped:Connect(function()
    handle.Ambience:Play()
    local fire = handle.Firecast:Clone()
    fire.Parent = player.Torso
    fire.Enabled = true
end)

script.Parent.Unequipped:Connect(function()
    handle.Ambience:Pause()
    player.Torso:FindFirstChild("Firecast"):Destroy()
end)

Thanks in advance.

2 answers

Log in to vote
0
Answered by 5 years ago

You are not properly finding the CharacterModel. From what I can see, in relation to the script within the tool, the CharacterModel should be script.Parent.Parent, you went one over.

Change line 1 in your top snippet of code to be

script.Parent.Equip.Player.Value = game.Players:FindFirstChild(script.Parent.Parent.Name)

Then change line 1 on your bottom snippet of code to be

player = script.Player.Value

The bottom snippet needs a change because the value of "Player" is the actual Player instance. This also means all references to the parts inside of your CharacterModel (based on how you attempted to do so) should be player.Character.BodyPartNameHere

Example being player.Character.Torso to reference torso, however saying Torso assume an R6 model of 'Robloxian'

Ad
Log in to vote
0
Answered by 5 years ago

When a tool is equipped its parent changes from the player backpack to the player's character model. Consider using this as an advantage.

Answer this question