Hello Devs! so, i was working on a simulator, but im facing a problem.
Im currently making some portals, i want that if the player has enough coins (“Clicks in my game”) , teleport him to some place
I tried this Code but its not working
local player = game.Players.LocalPlayer
local Clicks = player.leaderstats.Clicks.Value
script.Parent.Touched:Connect(function(hit)
local PlayerHit = hit.Parent
if PlayerHit then
local Torso = PlayerHit.HumanoidRootPart
if Torso then
if Clicks >= 500 then
Clicks -= 500
Torso.Position = Vector3.new(-502.543, 7.462, -227.165)
end
end
end
end)
You say you are using a Script but you’re calling LocalPlayer?
LocalPlayer can only be called from LocalScripts on the client
You would want to get the player inside your hit function using
local plr = Players:GetPlayerFromCharacter(PlayerHit)
It’ll return nil if it can’t find a player so you’d just have to change
if PlayerHit then
-- to
if plr then
(Also you’d have get the leaderstats and the clicks value inside)
Im not the best at scripting but i think ik why your code isnt working
i dont think local clicks = game.leaderstats.clicks.value works remove the .value and then when removing players clicks do clicks.value = clicks.value - 500
local DB = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")then
if DB == false then
DB = true
local char = hit.Parent
local plr = game.Players:GetPlayerFromCharacter(char)
if plr.leaderstats.Clicks.Value >= 500 then
plr.leaderstats.Clicks.Value -= 500
char.HumanoidRootPart.CFrame = CFrame.new(-502.543, 7.462, -227.165)
end
wait(1)
DB = false
end
end
end)